'use strict';

app.controller('viewMonthFinishCtrl', function($element, $scope, $state, rest, user, $rootScope, ngDialog, uiLoad, $timeout, $window, $ocLazyLoad, uploadConf, uploadprefix, tipService) {
    var data = angular.copy($scope.ngDialogData, {});
    var token = $window.localStorage.getItem("token");

    $scope.myData = {
        orderNo: data.orderNo,
        decorateRepaymentPlanId: data.id,
        planRepaymentDate: data.planRepaymentDate,
        periodNum: data.periodNum,
        planRepaymentAmount: data.planRepaymentAmount
    }

    $scope.$on('msg2', function(e, msg) {
        $scope.confirm({});
    });

    $scope.column2 = 0; //分栏
    $scope.maxImgNum = 1; //最大图片数量


    $scope.imgNew = [{
        text: '请选择照片',
        name: 'choosePhoto',
        value: [],
    }];
    uploadConf.column2 = $scope.column2;
    uploadConf.maxImgNum = $scope.maxImgNum;
    uploadConf.imgNew = $scope.imgNew;

    $scope.cancelRepay = function() {
        $scope.closeThisDialog();
    }

    rest.decoJava({
        type: "post",
        url: "/tenancy-sys/admin/decorateOrderInfo/queryDecorateRepaymentPlanById",
        data: {
            decorateRepaymentPlanId: $scope.myData.decorateRepaymentPlanId
        },
        beforeSend: function(request) {
            request.setRequestHeader("token", token);
        },
        success: function(data) {
            if (data.code == "0000") {
                $scope.$apply(function() {
                    $scope.queryParams = data.result;
                    var repayMon = Number($scope.myData.planRepaymentAmount) + Number($scope.queryParams.overdueFee);
                    $scope.queryParams.realRepaymentAmount = repayMon - Number($scope.queryParams.subtractOverdueFee);
                    $scope.queryParams.realRepaymentAmount = $scope.queryParams.realRepaymentAmount.toFixed(2);
                    var pushPhoto = function(photoString) {
                        var photoVals = [];
                        if (photoString && photoString.length > 0) {
                            photoVals = photoString.split(",");
                        }
                        return photoVals;
                    }
                    if ($scope.queryParams.repaymentVoucher == "null") {
                        $scope.imgNew[0].value = "";
                    } else if ($scope.queryParams.repaymentVoucher != "null") {
                        $scope.imgNew[0].value = pushPhoto($scope.queryParams.repaymentVoucher);
                    }
                })
            } else {
                tipService.show("body", {
                    message: data.message,
                    notice: "danger"
                }, 3);
            }

        }
    });

});